#include "Clist.h"
C_LIST C;
int extract = FALSE;
void addToRight(int x, C_LIST *cL)
{
    NODE *tmp;
    tmp = (NODE *)malloc(sizeof(NODE));
    tmp -> key = x;
    if (cL -> last == NULL)
    {
        cL -> last = tmp;
        cL -> first = tmp;
        cL -> last -> next = cL -> last;
        cL -> last -> prev = cL -> last;
    }
    else
    {
        tmp -> next = cL -> last -> next;
        tmp -> prev = cL -> last;
        cL -> last -> next -> prev = tmp;
        cL -> last -> next = tmp;
        cL -> last = tmp;
    }
    extract = TRUE;
}
void printC_list(C_LIST cL)
{
    NODE *tmp = cL.first;
    if(tmp == NULL)
        printf("List is empty.");
    else
        for(; tmp != cL.last; tmp = tmp -> next)
            printf("%d ", tmp -> key);
    printf("%d", cL.last -> key);
}
int deleteNode(NODE *tmp)
{
    int info = tmp -> key;
    if (tmp == C.first)
        C.first = tmp -> next;
    if (tmp == C.last)
        C.last = tmp -> prev;
    if (tmp -> next == tmp)
    {
        C.first = NULL;
        C.last = NULL;
        extract = FALSE;
    }
    else
    {
        tmp -> prev -> next = tmp -> next;
        tmp -> next -> prev = tmp -> prev;
    }
    free(tmp);
    return info;
}
int findSpot()
{
    int v[] = {1, 2, 3, 4, 5, 6, 7}, i = 0, k = 3, count = 1, x;
    for (; i < 7; ++i)
        addToRight(v[i], &C);
    printC_list(C);
    printf("\n");
    NODE *temp = C.first, *aux;
    for (; extract == TRUE; temp = temp -> next, ++count)
        if (count == k)
        {
            count = 1;
            aux = temp;
            temp = temp -> next;
            x = deleteNode(aux);
        }
    return x;
}

